package com.lemon.smartsafetynew.ui.activity.ledger;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafetynew.entity.common.DateTypeEnum;
import com.lemon.smartsafetynew.entity.ledger.LedgerBuildingItemVo;
import com.lemon.smartsafetynew.net.services.SafeLedgerServices;
import com.lemon.smartsafetynew.ui.adapter.ledger.LedgerBuildingListAdapter;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.user.AccountUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by Andrew on 2017/9/4.
 */

public class LedgerBuildingListActivity extends SwipeBackActivity
        implements RecyclerTClickListener<LedgerBuildingItemVo> {

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    private LedgerBuildingListAdapter mAdapter;
    private List<LedgerBuildingItemVo> mDataList = new ArrayList<>();

    @Override
    protected void initTitle() {
        this.setBarTitle("建筑列表");
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(LedgerBuildingListActivity.this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_ledger_building_list;
    }

    @Override
    protected void initViews() {
        super.initViews();

        initRecyclerView();

        getBuildingList();  //获取建筑列表
    }

    private void initRecyclerView() {

        mAdapter = new LedgerBuildingListAdapter(mDataList, LedgerBuildingListActivity.this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(LedgerBuildingListActivity.this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    public void onItemClick(LedgerBuildingItemVo data, int position) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(KeyConstants.KEY_LEDGER_PATROL_BUILDING_VO, data);
        UIHelper.showActivity(LedgerBuildingListActivity.this, bundle, LedgerBuildingCheckItemListActivity.class);
    }

    /**
     * 获取建筑列表
     */
    private void getBuildingList() {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("type", DateTypeEnum.DAY.getName().toLowerCase());
        params.put("date", CommonUtil.getNowTimeStr(DateTypeEnum.DAY));
        params.put("company_id", AccountUtil.getCompanyId());
        params.put("company_code", AccountUtil.getCompanyCode());

        SafeLedgerServices
                .init()
                .getBuildingList(params);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(HttpCallbackEvent event) {

        if (KeyConstants.URL_GET_SAFE_LEDGER_BUILDING_LIST.equals(event.getTag())) { //获取建筑列表
            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                mHUDUtil.show();
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow()) {
                mHUDUtil.hidden();
                if (Constants.Result.SUCCESS == event.getEventResult()) {

                    if (mAdapter != null) {
                        mDataList.clear();
                        mAdapter.notifyDataSetChanged();
                        mAdapter.addItems((List<LedgerBuildingItemVo>) event.getObj());
                    }

                } else if (Constants.Result.ERROR == event.getEventResult()) {

                    mHUDUtil.showErrorTip(event.getMsg(), new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {

                        }
                    });
                }
            }
        }
    }
}
